package com.java.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.CopyUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.system.dao.ResourceDao;
import com.java.system.entity.Resource;
import com.java.system.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("resourceService")
@Slf4j
public class ResourceServiceImpl extends ServiceImpl<ResourceDao, Resource> implements ResourceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Resource> page = this.page(
                new Query<Resource>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Resource>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存资源树
     */
    @Transactional
    public void saveJson(String json) {
        List<Resource> jsonList = JSON.parseArray(json, Resource.class);
        List<Resource> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(jsonList)) {
            // 最外层都是1级节点
            for (Resource d: jsonList) {
                d.setParent("");
                add(list, d);
            }
        }
        log.info("共{}条", list.size());

        this.remove(null);
        this.saveBatch(list);
    }

    /**
     * 递归，将树型结构的节点全部取出来，放到list
     */
    public void add(List<Resource> list, Resource resource) {
        list.add(resource);
        if (!CollectionUtils.isEmpty(resource.getChildren())) {
            for (Resource d: resource.getChildren()) {
                d.setParent(resource.getId());
                add(list, d);
            }
        }
    }

    /**
     * 按约定将列表转成树
     * 要求：ID要正序排列
     */
    public List<Resource> loadTree() {
        List<Resource> resourceList = this.list(new LambdaQueryWrapper<Resource>().orderByAsc(Resource::getId));
        for (int i = resourceList.size() - 1; i >= 0; i--) {
            // 当前要移动的记录
            Resource child = resourceList.get(i);

            // 如果当前节点没有父节点，则不用往下了
            if (StrUtil.isBlank(child.getParent())) {
                continue;
            }
            // 查找父节点
            for (int j = i - 1; j >= 0; j--) {
                Resource parent = resourceList.get(j);
                if (child.getParent().equals(parent.getId())) {
                    if (CollectionUtils.isEmpty(parent.getChildren())) {
                        parent.setChildren(new ArrayList<>());
                    }
                    // 添加到最前面，否则会变成倒序，因为循环是从后往前循环的
                    parent.getChildren().add(0, child);

                    // 子节点找到父节点后，删除列表中的子节点
                    resourceList.remove(child);
                }
            }
        }
        return resourceList;
    }

}